package cn.edu.cdu.farmdeal.controller;

import cn.edu.cdu.farmdeal.domain.QqLogin;
import cn.edu.cdu.farmdeal.domain.UserInf;
import cn.edu.cdu.farmdeal.dto.BindUserDto;
import cn.edu.cdu.farmdeal.service.AuthService;
import cn.edu.cdu.farmdeal.service.UserService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.session.Session;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author tupeng
 * @date 2019/12/08
 * @Description TODO
 **/
@Controller
public class AuthController {
    @Autowired
    private AuthService authService;
    @Autowired
    private QqLogin qqLogin;
    @Autowired
    private UserService userService;
    //   @Autowired
    //   private ThirdAppBindService thirdAppBindService;

    @RequestMapping(value = "/qqLoginPage", method = RequestMethod.GET)
    public String qqLogin(ModelAndView modelAndView) throws Exception {
        return authService.getAuthorizationUrl();

    }

    @GetMapping(value = "/oauth/qq/callback")
    @ResponseBody
    public void getQQCode(@RequestParam("code") String code, HttpServletRequest request, @NotNull HttpServletResponse response) throws Exception {
        System.out.println(code);
        //根据code获取token
        String accessToken = authService.getAccessToken(code);
        // 保存 accessToken 到 cookie，过期时间为 30 天，便于以后使用
        Cookie cookie = new Cookie("accessToken", accessToken);
        cookie.setMaxAge(60 * 24 * 30);
        response.addCookie(cookie);
        //本网站是将用户的唯一标识存在用户表中，大家也可以加一张表，存储用户和QQ的对应信息。
        //根据openId判断用户是否已经绑定过
        String openId = authService.getOpenId(accessToken);
        System.out.println(openId);
        BindUserDto user = (BindUserDto) authService.getUserInfo(accessToken, openId);
        HttpSession session=request.getSession();
        //session保存
        session.setAttribute("userid",user.getNickname());
        //检查数据库中是否存在该昵称
        UserInf userInf =new UserInf();
        if(!userService.checkUser(user.getNickname())){
            //将该昵称保存到数据库
            userInf.setUserid(user.getNickname());
            userInf.setPassword(new Md5Hash("123456", user.getNickname(), 1024).toHex());
            userInf.setEmail("765871131@qq.com");
            userInf.setName(user.getNickname());
            userService.register(userInf);
        }
        System.out.println(user.getNickname());
        System.out.println(user.getAvatar());
        System.out.println(user.getGender());
        if (user == null) {
            //如果用户不存在，则跳转到绑定页面
            response.sendRedirect("/static/login.html");
        } else {
            //如果用户已存在，则直接登录
            System.out.println(request.getContextPath());
            response.sendRedirect("http://localhost:8080/index.html");
        }
    }
}

